package com.hqbzl.c1.concurrs;

import lombok.extern.slf4j.Slf4j;

import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;

@Slf4j
public class TestConcurrent {
    public static void main(String[] args) {
        Map<String, AtomicInteger> values = new HashMap<>();
        Map<String, LongAdder> values1 = new HashMap<>();
        String str = "abcabd";
        final String[] split = str.split("");
        for (int i = 0; i < split.length; i++) {
            final String key = split[i];
            final int a1 = values.computeIfAbsent(key, v -> new AtomicInteger(0)).incrementAndGet();
            //final int a1 = values.computeIfAbsent(split[i], v -> new AtomicInteger(0)).getAndIncrement();
            if (a1 > 1) {
                System.out.println(key + " repeat " + a1);
            }
            values1.computeIfAbsent(key, k -> new LongAdder()).increment();
        }


        values.forEach((k, v) -> {
            System.out.println("k = " + k + " v:" + v);
        });
        values1.forEach((k, v) -> System.out.println(k + "\t" + v));
    }

    public static void main1(String[] args) {
        SynchronousQueue<String> a = new SynchronousQueue<>();
        new Thread(() -> {
            log.debug("get element");
            try {
                log.debug("a.take() {} ", a.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "get").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            a.put("1");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end");

    }
}
